﻿/***************************************************************************

Copyright (c) Microsoft Corporation. All rights reserved.
This code is licensed under the Visual Studio SDK license terms.
THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

***************************************************************************/

using System;
using Microsoft.VisualStudio.Project.Automation;
using Microsoft.VisualStudio.Project;
using System.IO;

namespace rfc1459.PS3Project
{
    /// <summary>
    /// This class extends the FileNode in order to represent a file 
    /// within the hierarchy.
    /// </summary>
    public class PlaystationFileNode : FileNode
    {
        #region Fields
        private OAPlaystationProjectFileItem automationObject;
        #endregion

        #region Constructors
        
        /// <summary>
        /// Initializes a new instance of the <see cref="PlaystationFileNode"/> class.
        /// </summary>
        /// <param name="root">The project node.</param>
        /// <param name="e">The project element node.</param>
        internal PlaystationFileNode(ProjectNode root, ProjectElement e)
            : base(root, e)
        {
        }

        #endregion

        #region Overriden implementation

        /// <summary>
        /// Gets the automation object for the file node.
        /// </summary>
        /// <returns></returns>
        public override object GetAutomationObject()
        {
            if (automationObject == null)
            {
                automationObject = new OAPlaystationProjectFileItem(this.ProjectMgr.GetAutomationObject() as OAProject, this);
            }

            return automationObject;
        }

        //protected internal override DocumentManager GetDocumentManager()
        //{
        //    return new PlaystationFileDocumentManager(this);
        //}

        public override object GetIconHandle(bool open)
        {
            if (Path.GetExtension(this.FileName).Equals(".P3P", StringComparison.CurrentCultureIgnoreCase))
            {
                return PS3Helpers.PublicResources.P3PFileIcon.Handle;
            }

            return base.GetIconHandle(open);
        }

        #endregion

        #region Private implementation

        internal OleServiceProvider.ServiceCreatorCallback ServiceCreator
        {
            get { return new OleServiceProvider.ServiceCreatorCallback(this.CreateServices); }
        }

        private object CreateServices(Type serviceType)
        {
            object service = null;
            if (typeof(EnvDTE.ProjectItem) == serviceType)
            {
                service = GetAutomationObject();
            }
            return service;
        }

        #endregion
    }
}
